define(window.backboneCommon.getViewDefine("targetPack.detail"), function ($,require,Collection,tpl,ViewTarget) {
    var backboneView= Backbone.View.extend({
        events: {
            "click #btn_submit": "saveSubmit"
        },
        initialize: function () {
            this.modelAttach={};
            this.modelAttach.baseData=this.model.get("modelBaseData");
            this.modelAttach.customer=this.model.get("modelCustomer");
            this.listenTo(this.modelAttach.baseData, "bind:ObjMatterType", this.bindObjMatterType);
            this.listenTo(this.modelAttach.customer, 'bind:AllList', this.bindCustomer);
            this.listenTo(this.model, 'bind:Info', this.bindInfo);
            this.listenTo(this.model, 'save:Info', this.saveInfoCallback);
            this.render();
        },
        render:function(){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml());
            return this;
        },
        initMain:function(id){
            this.elementModel={};
            this.extObject={};
            this.pageData={targetId:id};
            this.elementModel.form=$("#form_detail");
            this.viewAttach={};
            this._initPagePower();
        },
        _initPagePower:function(){
            var self=this;
            $.ExtAssist.rolePower.operatePage("create|edit",["advertiserList"],function(json){
                self.pageData.powerInfo=json;
                self._initBodyMain();
            });
        },
        _initBodyMain:function(){
            this.collection.getInfo(this.pageData.targetId);
            $.ExtAssist.urlParam.setUrl();
        },
        bindInfo:function () {
            var self=this;
            var result=this.model.get("dataInfo");
            self.pageData.isAdd=result.isAdd;
            $(".page-operate-title").text($.ExtCommon.getStringConnect("{0}定向包",self.pageData.isAdd?"新建":"编辑"));
            self.extObject.formValue=self.elementModel.form.ExtFormValue({initValue:result.res.data,
                initCallback:function(dataJson){
                    self.pageData.targetPackInfo=dataJson;
                    self.collection.initAfterDetail();
                    if(self.pageData.powerInfo["advertiserList"]){self.collection.getCustomer()}
                    self._initTarget(dataJson.targetData);
                    self.elementModel.form.ExtParsley();
                }
            });
            if (!result.res.success) {$.ExtNotify.error("该定向包数据获取失败：" +result.res.msg)}
        },
        bindCustomer:function () {
            var self=this;
            var res=this.modelAttach.customer.get("dataAllList");
            var list=$.ExtCommon.convertListByPara(res.data,{name:"nameExt"});
            self.extObject.customer= $("#ddl_customer").ExtSelect2({placeholder: "-请选择-",minimumResultsForSearch: 1,infoExt: {
                data: list,
                initCallback: function () {
                    if(!self.pageData.isAdd) {
                        self.extObject.customer.disabled()
                    }
                },
                changeCallback: function (value, element) {
                    self.elementModel.form.ExtParsley().validate(element);
                    self.viewAttach.target.deleteCrowd();
                }
            }});
        },
        bindObjMatterType:function () {
            var self=this;
            var res=self.modelAttach.baseData.get("dataObjMatterType");
            this.extObject.objMatterType=$("#panel_objMatterType").ExtRadioCheckbox({type: "radio",attrName:"objMatterType",data: res.data.list,
                initCallback:function () {
                    if(!self.pageData.isAdd) {
                        self.extObject.objMatterType.disabledAll();
                        self.changeObjMatterType();
                    }
                },
                changeCallback:function () {
                    self.viewAttach.target.deleteArea();
                    self.changeObjMatterType();
                }
            });
        },
        changeObjMatterType:function () {
            var info=this.extObject.objMatterType.getValueInfo();
            $("#container_target").prev().toggle(info.isWeChat)
        },

        _initTarget:function (json) {
            var self=this;
            self.viewAttach.target=new ViewTarget({pElement:"#container_target",
                selectData:json,
                checkCrowdCallback:function(){
                    $.ExtNotify.destroy();
                    var customer="";
                    if(self.pageData.powerInfo["advertiserList"]){
                        customer=self.extObject.customer.getValue();
                        if($.ExtCommon.isCheckEmpty(customer)){$.ExtNotify.error("请先选择所属广告主")}
                    }else{
                        customer="-1";
                    }
                    return customer;
                },
                checkAreaCallback:function () {
                    var info=self.extObject.objMatterType.getValueInfo();
                    return info.isWeChat?"group":"default";
                }
            });
        },

        _checkForm:function(targetData){
            $.ExtNotify.destroy();
            var isExtCheck=true;
            var objMatterInfo=this.extObject.objMatterType.getValueInfo();
            if(objMatterInfo.isWeChat) {
                if ($.ExtCommon.isCheckEmpty(targetData)) {
                    isExtCheck = false;
                    $.ExtNotify.error("请设置定向内容");
                } else {
                    if ($.ExtCommon.isCheckEmpty(targetData.area)) {
                        isExtCheck = false;
                        $.ExtNotify.error("微信品牌请设置地域定向");
                    }
                }
            }
            return this.elementModel.form.ExtParsley("validate") && isExtCheck;
        },
        saveSubmit:function () {
            var targetData= this.viewAttach.target.getValue();
            if(this._checkForm(targetData)){
                var infoOriginal = this.elementModel.form.ExtFormValue("getOriginalData");
                var ajaxData = this.elementModel.form.ExtFormValue("getValues");
                ajaxData.targetData=targetData;
                if(infoOriginal.id){ajaxData.id=infoOriginal.id}
                this.collection.saveInfo(ajaxData);
            }
        },
        saveInfoCallback:function () {
            var result=this.model.get("dataSave");
            if (result.res.success) {
                $.ExtNotify.success("保存成功!");
                if(result.isAdd){$.ExtAssist.pageListParam.setData("targetPackList",null)}
                $.ExtAssist.goUrlHref("#tool/target");
            }else{
                $.ExtNotify.error(result.res.msg);
            }
        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({ el: "#container_body",collection:this.collection,model:this.model});
    };
    viewModel.prototype={
        initView:function (param) {
            var id=param?param[0]:null;
            this.view.initMain(id);
        }
    };
    return viewModel;
});